<?php

	$this->setGuiMode("ajax");

	$errors = array();

	$images = explode(",", $_POST['fluffy_images']);
	if(count($images) != 2)
	{
		$errors['images'] = "<p>Please select two images</p>";
	}
	else
	{
		foreach($images as $image)
		{
			include_once('app/img/spam_pics/spam_image_map.php');
			if(!in_array($image, $regular_images))
				$errors['images'] = "<p>One of the animals selected was too fluffy</p>";
		}
	}
    if(empty($_POST['name']))
	{
		$errors['name'] = "<p>You must provide a name</p>";
	}
    if(empty($_POST['comment']))
	{
		$errors['comment'] = "<p>You must provide a comment</p>";
	}
	if(count($errors))
	{
		$message = "";
		foreach($errors as $error)
		{
			$message .= $error;
		}
		echo '{ "status": "failure", ';
		echo ' "message": "' . base64_encode($message) . '"}';
	}
	else
	{
		$args = array(
			'post_id' => $_POST['post_id'],
			'name' => $_POST['name'],
			'email' => $_POST['email'],
			'url' => $_POST['url'],
			'body' => $_POST['comment'],
			'date_added' => time(),
		);
		$comment = new Comment($args);
		$id = CommentMapper::get()->insert($comment);
		if($id)
		{
			echo '{ "status": "success", ';
			echo ' "id": "' . $id . '", ';
			$comment_plugin = Plugin::load('comment');
			$comment_plugin->setComment($comment);
			$contents = $comment_plugin->display();
			echo ' "message": "' . base64_encode($contents) . '"}';
			mail('smokinn@gmail.com', 'New blog comment', 'Comment By:' . $_POST['name'] . "\n\nComment:" . $_POST['comment'] . "\n\nhttp://smokinn.com/blog/post/" . $_POST['post_id']);
		}
		else
		{
			echo '{ "status": "failure", ';
			echo ' "message": "An unknown error occured"}';
		}
	}

	die();

?>